<?php
namespace Model;
class BloggList {
	private $database = null;
	const NOT_EMPTY = 0;
	const POST_PASSED = 1;
	const POST_FAILED = 2;
	const POST_DELETED = 3;
	const POST_NOT_DELETED = 4;
	const POST_SAVED = 5;
		//Skapa en konstruktor som tar en databas
	public function __construct($m_database) {
		$this->database = $m_database;
	}

	//Alla funktioner här är gjorde för att ta imot värden som man sedan kastar in i databasen och gör olika saker med databasen.
	public function getAllPosts() {
		$sql = "SELECT * FROM posts Order By postId Desc";
		$stmt = $this->database -> Select($sql);

		if ($stmt -> bind_result($postId, $date, $title, $post) == FALSE) {
			throw new Exception($this -> mysqli -> error);
		}

		$stmt -> bind_result($postId, $date, $title, $post);

		$array = new BloggArray();

		while ($stmt -> fetch()) {
			$blogg = new Blogg($postId, $date, $title, $post);
			$array -> add($blogg);
		}

		$stmt -> close();

		return $array;
	}
	//Bygger upp Html som skriver ut en post
	public function getPostByID($postId) {
		$sql = "Select * FROM posts Where postId = ?";
		$stmt = $this->database -> Prepare($sql);
		$stmt -> bind_param("i", $postId);
		// ska ske i databass klassen
		$stmt -> execute();
		if ($stmt -> bind_result($postId, $date, $title, $post) == FALSE) {
			throw new Exception($this -> mysqli -> error);
		}
		if ($stmt -> fetch()) {
			$ret = new Blogg($postId, $date, $title, $post);
		} else {
			throw new Exception("could not find a post with id $postId");
		}
		$stmt -> close();
		return $ret;
	}
	//Bygger upp Html form för ny post
	public function PostNewPost($title, $post) {
		if (isset($title) && isset($post)) {
			if ($title == "" || $post == '') {
				return self::NOT_EMPTY;
			}
			$vali = new \common\Validation();
			if ($vali -> filterJavascript($title, $post)) {
				if ($stmt = $this->database -> Prepare('INSERT INTO posts (title, post) VALUES (?,?)')) {
					$stmt -> bind_param('ss', $title, $post);
					$stmt -> execute();
					$stmt -> close();
					return self::POST_PASSED;
				}
			}
			return self::POST_FAILED;
		}
	}
	//Bygger upp Html form 
	public function DeletePost($id) {
		if (isset($id)) {

			if ($stmt = $this->database->Prepare('DELETE FROM posts WHERE postId = (?)'))
			{
				$stmt -> bind_param('i', $id);
				$stmt -> execute();
				$stmt -> close();
				return self::POST_DELETED;
			}
		}
		return self::POST_NOT_DELETED;
	}

	public function EditPost($id, $title, $post) {

		if ($title == "" || $post == '') {
			return self::NOT_EMPTY;
		}

		$vali = new \common\Validation();
		if ($vali -> filterJavascript($title, $post)) {
			if ($stmt = $this->database -> Prepare('UPDATE posts SET title = ? ,post = ? WHERE postId = ?')) {
				$stmt -> bind_param('ssi', $title, $post, $id);
				$stmt -> execute();
				$stmt -> close();
				return self::POST_SAVED;
			}
		}
		return self::POST_FAILED;

	}

}
